/*
 * Title: WADS
 * Description: Web-based Auto flow Documents System
 * Copyright: 2007
 * Company: CoE
 * @author: Mr.Nattasan Wang-Aphisit
 * @version 1.1
 */

package wads.app;

import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;

public class WADDiagramToolBar extends JToolBar{
//  private static final long serialVersionUID = 8293450281369813667L;
  
  private WADDiagramEditor editor;
  
  private SelectAction selectAction;
  private NodeAction nodeAction;
  private LinkAction linkAction;
  private DatabaseAction databaseAction;
  //private GroupAction groupAction;
  private GateAction gateAction;
  
  public static final int SELECT_TOOL = 0;
  public static final int NODE_TOOL = 1;
  public static final int LINK_TOOL = 2;
  public static final int DATABASE_TOOL = 3;
  public static final int GROUP_TOOL = 4;
  public static final int GATE_TOOL = 5;

  public WADDiagramToolBar(WADDiagramEditor ce) {
    super();
    editor = ce;
    
    selectAction = new SelectAction("", new ImageIcon("images/select.png"));
    nodeAction = new NodeAction("", new ImageIcon("images/user16.png"));
    linkAction = new LinkAction("", new ImageIcon("images/link.png"));
    databaseAction = new DatabaseAction("", new ImageIcon("images/database16.png"));
    //groupAction = new GroupAction("", new ImageIcon("images/group.gif"));
    gateAction = new GateAction("", new ImageIcon("images/gate16.png"));
    
    JButton jb;
    jb = add(selectAction);
    jb.setToolTipText("Select - Alt+Q");

    jb = add(nodeAction);
    jb.setToolTipText("Node - Alt+W");

    jb = add(linkAction);
    jb.setToolTipText("Link - Alt+E");

    jb = add(databaseAction);
    jb.setToolTipText("Database - Alt+R");
    
//    jb = add(groupAction);
//    jb.setToolTipText("Group - Alt+T");
    
    jb = add(gateAction);
    jb.setToolTipText("Gate - Alt+T");
    
    registerToolsKey();
  }
  
  private void registerToolsKey(){
    /* List of Tools Key Shortcut Setting:
     * Select tool   - Alt+Q
     * Node tool     - Alt+W
     * Link tool     - Alt+E
     * Database tool - Alt+R
     * Group tool    - Alt+[?] >> Deprecated
     * Gate tool     - Alt+T
     */
    
    //Select tool
    this.registerKeyboardAction(selectAction, KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.ALT_DOWN_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
    //Node tool
    this.registerKeyboardAction(nodeAction, KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.ALT_DOWN_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
    //Link tool
    this.registerKeyboardAction(linkAction, KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.ALT_DOWN_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
    //Database tool
    this.registerKeyboardAction(databaseAction, KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.ALT_DOWN_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
    //Gate tool
    this.registerKeyboardAction(gateAction, KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.ALT_DOWN_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
  }

  private class SelectAction extends AbstractAction {
//    private static final long serialVersionUID = -8990056658651996234L;

    public SelectAction(String name, Icon icon){
      super(name, icon);
    }

    @Override
    public void actionPerformed(ActionEvent event){
      System.out.println("Select Tool button clicked.");
      editor.setCurrentTool(WADDiagramToolBar.SELECT_TOOL);
    }
  }// end of selectAction Class

  private class NodeAction extends AbstractAction {
//    private static final long serialVersionUID = -4644091808302295748L;

    public NodeAction(String name, Icon icon){
      super(name, icon);
    }

    @Override
    public void actionPerformed(ActionEvent event){
        System.out.println("Node Tool button clicked.");
      editor.setCurrentTool(WADDiagramToolBar.NODE_TOOL);
    }
  }// end of nodeAction Class

  private class LinkAction extends AbstractAction {
//    private static final long serialVersionUID = 1312632249601092235L;

    public LinkAction(String name, Icon icon){
      super(name, icon);
    }

    @Override
    public void actionPerformed(ActionEvent event){
      System.out.println("Link Tool button clicked.");
      editor.setCurrentTool(WADDiagramToolBar.LINK_TOOL);
    }
  }// end of linkAction Class

  private class DatabaseAction extends AbstractAction {
//    private static final long serialVersionUID = -3674668212239520271L;

    public DatabaseAction(String name, Icon icon){
      super(name, icon);
    }

    @Override
    public void actionPerformed(ActionEvent event){
      System.out.println("Database Tool clicked.");
      editor.setCurrentTool(WADDiagramToolBar.DATABASE_TOOL);
    }
  }// end of databaseAction Class
  
  @Deprecated
  private class GroupAction extends AbstractAction{
//    private static final long serialVersionUID = -8957155565988483583L;

    public GroupAction(String name, Icon icon){
      super(name, icon);
    }
    
    @Override
    public void actionPerformed(ActionEvent event){
      System.out.println("Group Tool button clicked.");
      editor.setCurrentTool(WADDiagramToolBar.GROUP_TOOL);
    }
  }
  
  private class GateAction extends AbstractAction {
    public GateAction(String name, Icon icon) {
      super(name, icon);
    }
    
    @Override
    public void actionPerformed(ActionEvent event) {
      System.out.println("Gate Tool button clicked.");
      editor.setCurrentTool(WADDiagramToolBar.GATE_TOOL);
    }
  }
}